/**
 * Created by DevilsEyes on 2016/4/22.
 */
//noticeDetail
define(['layer', 'ex'], function (layer, ex) {
    'use strict';

    var MODE = {
        add: 1,
        edit: 2
    };
    var vm = avalon.define({
        $id: 'noticeDetail',

        mode: MODE.add,
        MODE: MODE, //判据

        _id: null,
        title: '',
        classSelected: [],
        classSelectList: [],
        article: '',
        send: false,

        __unwatch__: null,

        init: function () {
            vm.mode = MODE.add;
            vm.title = '';
            vm.send = false;

            vm.classSelected = [];
            $.ajax({
                url: '/api/admin/user/class',
                success: function (obj) {
                    if (obj.code == 0) {
                        vm.classSelectList = obj.list;

                        typeof vm.__unwatch__ == 'function' && vm.__unwatch__();
                        vm.article = '';
                        vm.__unwatch__ = vm.$watch('article', function () {
                            var t = $('textarea').get(0);
                            if (t.scrollHeight >= 80) {
                                t.style.height = t.scrollHeight + 'px';
                            } else {
                                t.style.height = 80;
                            }
                        });
                        setTimeout(function () {
                            $('[data-toggle="checkbox"]').radiocheck();
                        }, 0)
                    }
                }
            });
        },
        initEditMode: function (id) {
            vm._id = id;
            vm.mode = MODE.edit;
            var classReady = new Promise(function (res, rej) {
                $.ajax({
                    url: '/api/admin/user/class',
                    success: function (obj) {
                        if (obj.code == 0) {
                            vm.classSelectList = obj.list;
                            setTimeout(function () {
                                $('[data-toggle="checkbox"]').radiocheck();
                                res();
                            }, 0)
                        }
                    }
                });
            });
            $.ajax({
                url: '/api/admin/notice',
                data: {
                    _id: id
                },
                success: function (obj) {
                    if (obj.code != 0) {
                        layer.msg(obj.msg);
                        location.hash = '#!/notice/list';
                    } else {
                        var info = obj.info;

                        console.dir(info);
                        classReady.then(function () {
                            vm.classSelected = info.class.split('#');
                        });
                        vm.title = info.title;

                        typeof vm.__unwatch__ == 'function' && vm.__unwatch__();
                        vm.article = '';
                        vm.__unwatch__ = vm.$watch('article', function () {
                            var t = $('textarea').get(0);
                            if (t.scrollHeight >= 80) {
                                t.style.height = t.scrollHeight + 'px';
                            } else {
                                t.style.height = 80;
                            }
                        });
                        vm.article = info.article;

                    }
                }
            })
        },
        submit: function (e) {
            e.preventDefault();

            var data = {
                class: vm.classSelected.join('#'),
                title: vm.title,
                article: vm.article
            };

            if (data.title.length == 0) {
                return layer.msg('必须填写标题');
            }
            if (data.class.length == 0) {
                return layer.msg('必须选择班级');
            }
            if (data.article.length == 0) {
                return layer.msg('必须填写内容');
            }
            console.dir(data);

            switch (vm.mode) {
                case MODE.add:
                {
                    if (vm.send)data.send = vm.send;
                    $.ajax({
                        url: '/api/admin/notice',
                        method: 'POST',
                        data: {
                            json: JSON.stringify(data)
                        },
                        success: function (res) {
                            console.dir(res);
                            if (res.code == 0) {
                                layer.msg('新建公告成功!');
                                location.hash = '#!/notice/list';
                            }
                        }
                    });
                    return;
                }
                case MODE.edit:
                {
                    data._id = vm._id;
                    $.ajax({
                        url: '/api/admin/notice',
                        method: 'PUT',
                        data: {
                            json: JSON.stringify(data)
                        },
                        success: function (res) {
                            if (res.code == 0) {
                                layer.msg('编辑公告成功!');
                                location.hash = '#!/notice/list';
                            }
                        }
                    });
                }
            }


        }
    });

    return {
        add: function () {
            vm.init();
            // $root.page = temp;
        },
        edit: function (id) {
            vm.initEditMode(id);
        }
    }
});